/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip;

import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.sip.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Iterator;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class SipStackImplTest extends TestCase {
    private final static Log logger = LogFactory.getLog("SipStackImplTest");

    public SipStackImplTest(String name) {
        super(name);
    }

    final static int PORT_NUMBER = 10000;

    public void test() {
        final SipFactory factory = SipFactory.getInstance();
        factory.setPathName("com.jnetx");
        final Properties props = new Properties();
        props.setProperty("javax.sip.STACK_NAME", "Deps SIP Stack v0.1");
        //Current IP address
        try {
            InetAddress local = InetAddress.getLocalHost();
            props.setProperty("com.jnetx.javax.sip.IP_ADDRESS", local.getHostAddress());
        } catch (UnknownHostException e) {
            final String message = "Cannot get the local host's IP address!";
            logger.error(message, e);
            fail(message);
            return;
        }
        final SipStack stack;
        try {
            stack = SipFactory.getInstance().createSipStack(props);
        } catch (PeerUnavailableException e) {
            final String message = "Cannot create SIP stack!";
            logger.error(message, e);
            fail(message);
            return;
        }
        assertNotNull("The created SIP stack object is null!", stack);
        ListeningPoint lPoint;
        //There is no IP address specified in the properties so the following method has to throw an exception
        try {
            lPoint = stack.createListeningPoint(PORT_NUMBER, "UDP");
            fail("The createListening method has to throw an exception if there is no IP address specified in the properties!");
        } catch (TransportNotSupportedException e) {
        } catch (InvalidArgumentException e) {
            final String message = "Cannot create a listening point!";
            logger.error(message, e);
            fail(message);
            return;
        }
        try {
            lPoint = stack.createListeningPoint(getLocalIPAddress(), PORT_NUMBER, "UDP");
        } catch (Exception e) {
            final String message = "";
            logger.error(message, e);
            fail(message);
            return;
        }
        assertNotNull("The created listening point is null!", lPoint);
        final SipProvider provider;
        try {
            provider = stack.createSipProvider(lPoint);
        } catch (ObjectInUseException e) {
            final String message = "Cannot create a SIP provider!";
            logger.error(message, e);
            fail(message);
            return;
        }
        assertNotNull("The created provider object is null!", provider);
        Iterator lpIter = stack.getListeningPoints();
        assertTrue("There should be a listening point!", lpIter.hasNext());
        ListeningPoint lp = (ListeningPoint) lpIter.next();
        assertSame("The listening point is not the same!", lPoint, lp);
        assertFalse("There shouldn't be any more listening points!", lpIter.hasNext());

        final String ip = stack.getIPAddress();
        assertNull("The default IP address should be null!", ip);

        try {
            stack.deleteSipProvider(provider);
        } catch (ObjectInUseException e) {
            final String message = "Cannot delete the SIP provider!";
            logger.error(message, e);
            fail(message);
        }
        try {
            stack.deleteListeningPoint(lPoint);
        } catch (ObjectInUseException e) {
            final String message = "Cannot delete the listening point!";
            logger.error(message, e);
            fail(message);
        }
        lpIter = stack.getListeningPoints();
        assertFalse("There shouldn't be any listening points!", lpIter.hasNext());
    }

    private static String getLocalIPAddress() {
        final String result;
        //Current IP address
        try {
            InetAddress local = InetAddress.getLocalHost();
            result = local.getHostAddress();
        } catch (UnknownHostException e) {
            logger.error("Cannot get the local host's IP address!", e);
            fail("Cannot get the current IP address!");
            return null;
        }
        return result;
    }
}
/*
 * $Log$
 */